<?php 

/**
 * 文件上传Widget
 * @author  chengx impl@wanthings.com
 * 
 */
class FileUploadWidget extends CWidget
{
    public $fileName = null;
    public $fileLabel = null;
    public $fileId = null;
    public $isImage = 0;
    public $imageWidth = '200px';
    public $imageUrl = '';
    public function init()
    {
        parent::init();
    }

    public function run()
    {
        if (is_null($this->fileName) || is_null($this->fileLabel)) {
            throw new CHttpException('初始化错误', 500);
        }
        if (is_null($this->fileId)) {
            $this->fileId = $this->getDomId();
        }
        $this->render('fileUpload', array('isImage' => $this->isImage, 'fileName' => $this->fileName, 'fileLabel' => $this->fileLabel, 'fileId' => $this->fileId, 'imageWidth' => $this->imageWidth, 'imageUrl' => $this->imageUrl));
    }

    private function getDomId()
    {
        $id = Yii::app()->cache->get('_file_id_');
        if (!$id) {
            $id = 0;
        }
        if (!Yii::app()->cache->set('_file_id_', $id + 1, 60)) {
            throw new CHttpException('缓存错误，请手动设定fileId');
        }
        return '_file_id_'. $id;
    }
}